#include "PassiveCtrl.h"
#include "SocketIO.h"
#include "Node.h"
#include <iostream>
#include <thread>
namespace task {
    int PassiveCtrl::clientAction(socketinf serverinf, const char * arg, size_t len) {
        // Auto generate
        SocketIO sio(serverinf);
        remoteMaster.setSocket(serverinf);
        // IMPORTANT trigger master execute serverAction 
        sio.socketWriter->sendFrame(TaskFlag::passivectrl, 0);
        sio.socketReader->readFrame();
        
        // 启动一个线程向master发送心跳包和接收控制帧
        std::thread th(&RemoteMaster::sendDoki, &remoteMaster),th2(&RemoteMaster::acceptControl, &remoteMaster);
        th.detach();th2.detach();
        std::cout << "Connect Sccess" << std::endl;
        return KEEP_ALIVE;
    }
    int PassiveCtrl::serverAction(socketinf clientinf, unsigned long long dataLength) {
        // Auto generate
        SocketIO sio(clientinf);
        sio.socketWriter->sendFrame(TaskStat::success, 0);
        localMaster.addSlave(new Slave(clientinf));
        // 不通过任务控制器释放连接
        return KEEP_ALIVE;
    }
}
